from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import cv2
from win import Ui_Form
from PyQt5.QtCore import Qt
from info import Info_win
class MyInfoWin(QMainWindow, Info_win):
    def __init__(self):
        super(MyInfoWin, self).__init__()
        self.setupUi(self)
class Window(QMainWindow, Ui_Form):
    def __init__(self):
        super(Window, self).__init__()
        self.setupUi(self)
        self.folderPath = ''
        self.tb_result.append("软件初始化成功")
        self.btn_start.clicked.connect(self.go)
        self.btn_info.clicked.connect(self.showInfo)
        self.infoWin=MyInfoWin()
        
        self.timer_camera = QtCore.QTimer()  # 定义定时器，用于控制显示视频的帧率
        self.timer_camera.timeout.connect(self.show_camera)
        self.cap = cv2.VideoCapture()  # 视频流
        self.CAM_NUM = 0  # 为0时表示视频流来自笔记本内置摄像头

    def showInfo(self):
        self.infoWin.show()

    def go(self):
        if self.timer_camera.isActive() == False:  # 若定时器未启动
            flag = self.cap.open(self.CAM_NUM)  # 参数是0，表示打开笔记本的内置摄像头，参数是视频文件路径则打开视频
            if flag == False:  # flag表示open()成不成功
                self.tb_result.append("请检查相机于电脑是否连接正确")
            else:
                self.timer_camera.start(30)  # 定时器开始计时30ms，结果是每过30ms从摄像头中取一帧显示
        else:
            self.timer_camera.stop()  # 关闭定时器
            self.cap.release()  # 释放视频流
            self.label_screen.clear()  # 清空视频显示区域
 
    def show_camera(self):
        flag, self.image = self.cap.read()  # 从视频流中读取
 
        show = cv2.resize(self.image, (300, 250))  # 把读到的帧的大小重新设置为 640x480
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)  # 视频色彩转换回RGB，这样才是现实的颜色
        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],
                                 QtGui.QImage.Format_RGB888)  # 把读取到的视频数据变成QImage形式
        self.label_screen.setPixmap(QtGui.QPixmap.fromImage(showImage))  # 往显示视频的Label里 显示QImage
 

if __name__ == '__main__':
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    father = Window()
    father.show()
    sys.exit(app.exec_())
